package com.union.vehicleassistant.util;

import android.content.Context;
import android.text.TextUtils;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.tencent.mmkv.MMKV;
import com.union.vehicleassistant.http.api.UserInfoApi;

import java.util.Objects;

/**
 * 用户登录管理
 *
 * @author mz
 * @date-time 2024/11/20 9:24
 */
public class UserUtils {

    private static final String KEY_LOGIN_USER_ENTITY = "KEY_LOGIN_USER_ENTITY";
    private static final String KEY_LOGIN_TOKEN = "KEY_LOGIN_TOKEN";
    private static final String KEY_ACCOUNT = "KEY_ACCOUNT";
    private static final String KEY_PASS = "KEY_PASS";
    private static final String KEY_VPN_ACCOUNT = "KEY_VPN_ACCOUNT";
    private static final String KEY_VPN_PASS = "KEY_VPN_PASS";
    private static final String KEY_VPN_ADDRESS = "KEY_VPN_ADDRESS";
    private static final String VPN_ADDRESS = "https://218.12.92.76:60443";

    private UserInfoApi.Bean mUserEntity = null;

    private static class Holder {
        private static final UserUtils INSTANCE = new UserUtils();
    }

    public static UserUtils getInstance() {
        return Holder.INSTANCE;
    }

    private UserUtils() {
        getLoginUser();
    }

    public UserInfoApi.Bean getLoginUser() {
        if (mUserEntity == null) {
            String json = MMKV.defaultMMKV().getString(KEY_LOGIN_USER_ENTITY, "");
            if (!TextUtils.isEmpty(json)) {
                try {
                    mUserEntity = new Gson().fromJson(json, UserInfoApi.Bean.class);
                } catch (Exception ignore) {
                }
            }
        }
        return mUserEntity;
    }


    public void setToken(String token) {
        MMKV.defaultMMKV().putString(KEY_LOGIN_TOKEN, token);
    }

    public String getToken() {
        return MMKV.defaultMMKV().getString(KEY_LOGIN_TOKEN, "");
    }

    public void logout() {
        mUserEntity = null;
        MMKV.defaultMMKV().removeValuesForKeys(new String[]{KEY_LOGIN_USER_ENTITY, KEY_LOGIN_TOKEN});
    }

    public void updateInfo(UserInfoApi.Bean userEntity) {
        mUserEntity = userEntity;
        MMKV.defaultMMKV().putString(KEY_LOGIN_USER_ENTITY, new Gson().toJson(mUserEntity));
    }

    public boolean isLogin() {
        String token = MMKV.defaultMMKV().getString(KEY_LOGIN_TOKEN, "");
        return !TextUtils.isEmpty(token);
    }

    public int getUserId() {
        UserInfoApi.Bean userEntity = getLoginUser();
        return Objects.isNull(userEntity) ? -1 : userEntity.getId();
    }

    public boolean doIfLogin(Context context) {
        if (isLogin()) {
            return true;
        } else {
//            AuthActivity.startQuickLogin(context);
            return false;
        }
    }

    public void setAccount(String account) {
        SPUtils.getInstance().put(KEY_ACCOUNT, account);
    }

    public String getAccount() {
        return SPUtils.getInstance().getString(KEY_ACCOUNT);
    }

    public void setPass(String pass) {
        SPUtils.getInstance().put(KEY_PASS, pass);
    }

    public String getPass() {
        return SPUtils.getInstance().getString(KEY_PASS, "");
    }

    public void setVPNAccount(String account) {
        SPUtils.getInstance().put(KEY_VPN_ACCOUNT, account);
    }

    public String getVPNAccount() {
        return SPUtils.getInstance().getString(KEY_VPN_ACCOUNT);
    }

    public void setVPNPass(String pass) {
        SPUtils.getInstance().put(KEY_VPN_PASS, pass);
    }

    public String getVPNPass() {
        return SPUtils.getInstance().getString(KEY_VPN_PASS, "");
    }

    public void setVPNAddress(String address) {
        SPUtils.getInstance().put(KEY_VPN_ADDRESS, address);
    }

    public String getVPNAddress() {
        return SPUtils.getInstance().getString(KEY_VPN_ADDRESS, VPN_ADDRESS);
    }
}
